package org.bing.engine.utility.codec;

import java.sql.Timestamp;
import java.util.Date;

public class SimpleTypeHelper {

    public boolean isSimpleType(Object obj) {
        return isSimpleType(obj.getClass());
    }

    @SuppressWarnings({ "rawtypes" })
    public boolean isSimpleType(Class clazz) {
        if (clazz.isPrimitive()) {
            return true;
        }
        if (clazz == String.class || clazz == Date.class) {
            return true;
        }

        if (Boolean.class == clazz || Character.class == clazz || Byte.class == clazz || Short.class == clazz)
            return true;
        if (Integer.class == clazz || Long.class == clazz || Float.class == clazz || Double.class == clazz)
            return true;
        if (Timestamp.class == clazz) {
            return true;
        }
        return false;
    }

    public boolean isSimpleType(String clzName) {
        try {
            if ("string".equals(clzName) || "date".equals(clzName)) {
                return true;
            } else if ("boolean".equals(clzName) || "char".equals(clzName) || "byte".equals(clzName) || "short".equals(clzName)
                    || "int".equals(clzName) || "long".equals(clzName) || "float".equals(clzName) || "double".equals(clzName)) {
                return true;

            } else {
                return isSimpleType(Class.forName(clzName));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();// TODO remove
            return false;
        }
    }

    @SuppressWarnings({ "rawtypes" })
    public String fineClzName(Class clz) {
        if (clz.isPrimitive())
            return clz.getName();
        if (Boolean.class == clz)
            return "boolean";
        else if (Character.class == clz)
            return "char";
        else if (Byte.class == clz)
            return "byte";
        else if (Short.class == clz)
            return "short";
        else if (Integer.class == clz)
            return "int";
        else if (Long.class == clz)
            return "long";
        else if (Float.class == clz)
            return "float";
        else if (Double.class == clz)
            return "double";
        else if (String.class == clz)
            return "string";
        else if (Date.class == clz)
            return "date";
        else if (Timestamp.class == clz)
            return "date";
        return clz.getName();
    }
}
